package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.web.domain.vo.GuTagVo;
import com.ruoyi.web.domain.vo.GuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.GuGupiaoMapper;
import com.ruoyi.web.domain.GuGupiao;
import com.ruoyi.web.service.IGuGupiaoService;

/**
 * 股票库Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class GuGupiaoServiceImpl implements IGuGupiaoService 
{
    @Autowired
    private GuGupiaoMapper guGupiaoMapper;

    /**
     * 查询股票库
     * 
     * @param id 股票库主键
     * @return 股票库
     */
    @Override
    public GuGupiao selectGuGupiaoById(String id)
    {
        return guGupiaoMapper.selectGuGupiaoById(id);
    }

    /**
     * 查询股票库列表
     * 
     * @param guGupiao 股票库
     * @return 股票库
     */
    @Override
    public List<GuGupiao> selectGuGupiaoList(GuGupiao guGupiao)
    {
        return guGupiaoMapper.selectGuGupiaoList(guGupiao);
    }

    /**
     * 新增股票库
     * 
     * @param guGupiao 股票库
     * @return 结果
     */
    @Override
    public int insertGuGupiao(GuGupiao guGupiao)
    {
        guGupiao.setCreateTime(DateUtils.getNowDate());
        guGupiao.setId(UUID.randomUUID().toString().replace("-", ""));
        return guGupiaoMapper.insertGuGupiao(guGupiao);
    }

    /**
     * 修改股票库
     * 
     * @param guGupiao 股票库
     * @return 结果
     */
    @Override
    public int updateGuGupiao(GuGupiao guGupiao)
    {
        guGupiao.setUpdateTime(DateUtils.getNowDate());
        return guGupiaoMapper.updateGuGupiao(guGupiao);
    }

    /**
     * 批量删除股票库
     * 
     * @param ids 需要删除的股票库主键
     * @return 结果
     */
    @Override
    public int deleteGuGupiaoByIds(String[] ids)
    {
        return guGupiaoMapper.deleteGuGupiaoByIds(ids);
    }

    /**
     * 删除股票库信息
     * 
     * @param id 股票库主键
     * @return 结果
     */
    @Override
    public int deleteGuGupiaoById(String id)
    {
        return guGupiaoMapper.deleteGuGupiaoById(id);
    }

    @Override
    public String importDate(List<GuGupiao> gupiaoList, String operName) {
        if (StringUtils.isNull(gupiaoList) || gupiaoList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GuGupiao gu : gupiaoList){

            try{
                GuGupiao guGupiao = guGupiaoMapper.selectGuGupiaoByguCode(gu.getGuCode());
                if( StringUtils.isNull(guGupiao)){
                    gu.setCreateTime(DateUtils.getNowDate());
                    gu.setId(UUID.randomUUID().toString().replace("-", ""));
                    guGupiaoMapper.insertGuGupiao(gu);
                    successNum++;
                }else {
                    gu.setUpdateTime(DateUtils.getNowDate());
                    guGupiaoMapper.updateGuGupiaoByGuCode(gu);

                    successNum++;
                }
            } catch (Exception e) {
               failureNum++;
                String msg = "<br/>" + failureNum + "、股票代码： " + gu.getGuCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条!");
        }
        return successMsg.toString();
    }

    @Override
    public List<GuGupiao> selectGuGupiaoListAll() {
        return guGupiaoMapper.selectGuGupiaoListAll();
    }

    @Override
    public int updateGuGupiaoTag(GuTagVo guTagVo) {
        List<GuVo> guVoList = guTagVo.getStocks_list();
       for (GuVo guVo : guVoList) {
           String stock_code =keepLetters( guVo.getStock_code())+keepDigits(guVo.getStock_code());
            GuGupiao   guGupiao =  guGupiaoMapper.selectGuGupiaoByguCode(stock_code);
            if(StringUtils.isNull(guGupiao.getGuTagId())){
                guGupiao.setGuTagId(guTagVo.getOffset());
            }else {
                guGupiao.setGuTagId(guGupiao.getGuTagId()+","+guTagVo.getOffset());
            }
            guGupiao.setGuPriceName(guVo.getLiutong_cap());
            guGupiaoMapper.updateGuGupiaoByGuCode(guGupiao);
       }

        return 0;
    }
    public static String keepLetters(String input) {
        return input == null ? null : input.replaceAll("[^a-zA-Z]", "");
    }
    public static String keepDigits(String input) {
        return input == null ? null : input.replaceAll("[^0-9]", "");
    }
}
